home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11016 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  60 lines

  1. Path: news.cc.tut.fi!olkikukka!csromu
  2. From: csromu@olkikukka.uta.fi (Roland Mueller)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Need Unique ID generation
  5. Date: 12 Mar 1996 07:02:51 GMT
  6. Organization: University of Tampere, Finland
  7. Distribution: world
  8. Message-ID: <4i37ir$d4f@cc.tut.fi>
  9. References: <4h3vmq$4a6@alpha.it.net> <31369697.249D@symantec.com> <4h7b4k$837@news.halcyon.com> <4hf6t9$f6n@lib103.its.rpi.edu>
  10. NNTP-Posting-Host: olkikukka.uta.fi
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Barry B Floyd (floydb1@lib103.its.rpi.edu) wrote:
  14.  
  15. : Has this problem been solved without using a network card ID (i.e.
  16. : for computers w/o network cards)?
  17.  
  18. Assuming that you want to provide unique IDs at application level
  19. you may create a class for ID and hide the constructor as
  20. private/protected. The only way to get an new ID, i.e.
  21. an instance of this class, is to call a static function.
  22.  
  23. Ex.
  24. template<ID_Type>
  25. class ID
  26. {
  27. public:
  28.   // of course you may also overwrite the new operator, 
  29.   // but I forgot the precise syntax for that.
  30.   static ID &New();
  31.  
  32. private:
  33.   ID(const ID_Type);
  34.   ID_Type id;
  35.   static ID_Type last_id;
  36. };
  37.  
  38. template<ID_Type>
  39. ID<ID_Type>::last_id = 0; // or your init value
  40.  
  41. template<ID_Type>
  42. ID<ID_Type> &ID<ID_Type>::New()
  43. {
  44.   
  45.   return *new (last_id++); // your type must provide ++operator
  46. }
  47.  
  48. template<ID_Type>
  49. ID<ID_Type>::ID(const ID_Type new_id)
  50. {
  51.   id = new_id;
  52. }
  53.  
  54.  
  55.  
  56. --
  57. Roland Mⁿller     KΣrΣjΣt÷rmΣ 4 B 15, 33310 Tampere/Finland
  58. phone         +358-31-3453609 
  59. e-mail        Roland.Mueller@uta.fi
  60.